{extend name="public/base" /}

{block name="css"}
<style>
    .layui-table-cell {
        height:auto;
    }
</style>
{/block}

{block name="title"}控制台{/block}

{block name="main"}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
            <div class="layui-upload-list">
                <table class="layui-table">
                    <thead>
                    <tr><th>文件名</th>
                        <th>预览</th>
                        <th>大小</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr></thead>
                    <tbody id="demoList"></tbody>
                </table>
            </div>
            <button type="button" class="layui-btn" id="testListAction">开始上传</button>
        </div>
    </div>

    <div class="layui-card" style="margin-top: 20px;">
        <div class="layui-card-header">图片列表</div>
        <div class="layui-card-body">
            <table class="layui-hide" id="product-images" lay-filter="product-images"></table>
        </div>
    </div>
</div>

<script type="text/html" id="image">
    <img class="layui-upload-img logo" src="{{d.image}}" />
</script>

{/block}

{block name="js"}
<script>
    layui.use(['form', 'layer', 'upload', 'admin', 'table'], function(){
        var $ = layui.jquery
            , layer = layui.layer
            , admin = layui.admin
            , form = layui.form
            , table = layui.table
            , upload = layui.upload;

        get_image_list();

        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引

        table.on('edit(product-images)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            var _load_index = layer.load();
            admin.req({
                url: "{:url('save_image_info')}" //实际使用请改成服务端真实接口
                ,data: {
                    field: obj.field,
                    value: obj.value,
                    id: obj.data.id
                }
                ,done: function(res){
                    //登入成功的提示与跳转
                    layer.msg('修改成功');
                    get_image_list();
                }
            });
        });

        //多文件列表示例
        var demoListView = $('#demoList')
            ,uploadListIns = upload.render({
            elem: '#testList'
            ,url: '{:url("save_image")}'
            ,data: {
                id: '{$data.id}',
                type: '{$type}'
            }
            ,accept: 'file'
            ,multiple: true
            ,auto: false
            ,bindAction: '#testListAction'
            ,choose: function(obj){
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                console.log(files.length);
                //读取本地文件
                obj.preview(function(index, file, result){
                    var tr = $(['<tr id="upload-'+ index +'" class="img" data-index="'+ index +'">'
                        ,'<td>'+ file.name +'</td>'
                        ,'<td><img style="max-height: 200px;" src="'+result+'" /></td>'
                        ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                        ,'<td>等待上传</td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    demoListView.append(tr);
                });
            }
            ,done: function(res, index, upload){
                if(res.code == 0){ //上传成功
                    var tr = demoListView.find('tr#upload-'+ index)
                        ,tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作
                    delete this.files[index]; //删除文件队列已经上传成功的文件

                    if ($(".img").last().data('index') == index) {
                        get_image_list();
                    }

                    return;
                }
                this.error(index, upload);
            }
            ,error: function(index, upload){
                var tr = demoListView.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });

        function get_image_list() {
            table.render({
                elem: '#product-images'
                ,url: '{:url("image_list")}'
                ,where: {
                    id: '{$data.id}',
                    type: '{$type}'
                }
                ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                ,cols: [[
                    {field:'id', width:50, title: 'ID'}
                    ,{field:'name', width: 300, title: '名称（点击修改）', edit: 'text'} //width 支持：数字、百分比和不填写。你还可以通过 minWidth 参数局部定义当前单元格的最小宽度，layui 2.2.1 新增
                    ,{field:'', width: 200, title: '图片', templet: '#image'}
                    ,{field:'sort', title: '排序（点击修改，值越大越靠后）', edit: 'text'}
                ]]
                , done: function () {
                    layer.closeAll();
                }
            });
        }
    });
</script>
{/block}